package com.matchmaking.system.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.matchmaking.system.domain.SysDictType;

import java.util.ArrayList;
import java.util.List;

public class SysDictListener extends AnalysisEventListener {

    //集合
    List<String> list = new ArrayList<>();

    @Override
    public void invoke(Object data, AnalysisContext analysisContext) {
        String dictName = ((SysDictType) data).getDictName();
        list.add(dictName);
        System.err.println("导入一条数据成功!" + ((SysDictType) data));
    }


    @Override
    public void onException(Exception exception,AnalysisContext context) throws Exception {
        if(exception instanceof ExcelDataConvertException){
            /*从0开始计算*/
            int columnIndex = ((ExcelDataConvertException) exception).getColumnIndex() + 1;
            int rowIndex = ((ExcelDataConvertException) exception).getRowIndex() + 1;
            String message = "第" + rowIndex + "行,第" + columnIndex + "列" + "数据格式有误,请核实";
            throw new RuntimeException(message);
        } else if (exception instanceof ExcelDataConvertException) {
            throw exception;
        }else {
            super.onException(exception,context);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //解析完,全部回调逻辑实现
        System.err.println("一共导入了" + list.size() + "条数据");
        list.clear();
    }
}
